package cn.tedu.luban.order.main.controller;

import cn.tedu.luban.order.main.converter.OrderConverter;
import cn.tedu.luban.order.main.service.OrderFrontService;
import cn.tedu.luban.order.protocol.VO.FrontOrderDetailVO;
import cn.tedu.luban.order.protocol.VO.FrontOrderItemVO;
import cn.tedu.luban.order.protocol.bo.OrderBO;
import cn.tedu.luban.order.protocol.param.OrderConfirmParam;
import cn.tedu.luban.order.protocol.param.OrderCreateParam;
import cn.tedu.luban.order.protocol.param.OrderFinishParam;
import cn.tedu.luban.order.protocol.param.OrderSignParam;
import cn.tedu.luban.order.protocol.query.FrontOrderListQuery;
import com.tedu.inn.protocol.ListRecordTotalBO;
import com.tedu.inn.protocol.exception.BusinessException;
import com.tedu.inn.protocol.model.Result;
import com.tedu.inn.protocol.pager.PagerResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
@Api(value = "订单前台接口",tags = "订单前台接口")
@RestController
public class OrderFrontController {
    @Autowired
    private OrderFrontService orderFrontService;
    @Autowired
    private OrderConverter orderConverter;
    /**
     * 抢单下单操作
     */
    @ApiOperation("前台抢单")
    @PostMapping("/order/create")
    public Result<String> orderCreate(@RequestBody OrderCreateParam orderCreateParam) throws BusinessException {
        String orderNO=orderFrontService.orderCreate(orderCreateParam);
        return new Result(orderNO);
    }
    /**
     * 订单列表
     */
    @ApiOperation("前台订单列表")
    @PostMapping("/order/list")
    public Result<PagerResult<FrontOrderItemVO>> orderList
    (@RequestBody FrontOrderListQuery orderQuery)throws BusinessException{
        //拿到业务层ListRecordTotalBO
        ListRecordTotalBO<OrderBO> orderBOS=orderFrontService.orderList(orderQuery);
        //转化成PagerResult
        PagerResult<FrontOrderItemVO> pageResult = orderConverter.assemblePagerResult(orderBOS,orderQuery);
        return new Result<>(pageResult);
    }
    /**
     * 订单详情
     */
    @ApiOperation("前台订单详情")
    @GetMapping("/order/info")
    public Result<FrontOrderDetailVO> orderDetail(String orderNo)throws BusinessException{
        //业务返回BO
        OrderBO orderBO=orderFrontService.orderDetail(orderNo);
        //转化VO
        FrontOrderDetailVO orderDetailVO = orderConverter.bo2frontDetailVO(orderBO);
        return new Result<>(orderDetailVO);
    }
    @ApiOperation("前台订单签到")
    @PostMapping("/order/sign")
    public Result<String> orderSign(@RequestBody OrderSignParam orderSignParam)
        throws BusinessException{
        orderFrontService.orderSign(orderSignParam);
        return new Result<>(orderSignParam.getOrderNo());
    }

    /**
     * 绑定施工图片
     */
    @ApiOperation("前台订单施工图片确认")
    @PostMapping("/order/confirm")
    public Result<String> orderConfirm(@RequestBody OrderConfirmParam orderConfirmParam)
        throws BusinessException{
        //直接调用业务代码 处理绑定
        orderFrontService.orderConfirm(orderConfirmParam);
        return new Result<>(orderConfirmParam.getOrderNo());
    }
    @ApiOperation("前台订单完成")
    @PostMapping("/order/finish")
    public Result<String> orderFinish(
        @RequestBody OrderFinishParam orderFinishParam)
        throws BusinessException{
        orderFrontService.orderFinish(orderFinishParam);
        return new Result<>(orderFinishParam.getOrderNo());
    }
}
